<template>
    <eui-page-frame topHeight="170" :bottomHeight="bottomHeight">
        <template #top>
            <view class="navigation">
                <eui-nav-bar :title="title" color="#000000" rightIcon="" />
            </view>
        </template>
        <eui-page-content>
            <eui-base-card v-for="(item, index) in showdata" :key="index">
                <eui-card-input v-model="item.c_value" :range="item?.c_option_list" :title="item?.c_title"
                    :type="item.c_input_type" :displayable="item.c_displayable" />

                <eui-card-input v-model="item.c_is_abnormal" title="是否异常" type="switch"
                    :displayable="item.c_displayable" />

                <eui-card-input v-show="item.c_is_abnormal == '1'" v-model="item.c_rectify_memo" title="整改说明"
                    type="textarea" placeholder="请录入整改说明" maxlength="200" :displayable="item.c_displayable" />

                <eui-card-input v-if="item.c_work_status >= '5' && item.c_is_abnormal == '1'"
                    v-model="item.c_is_recheck" title="复检完成" type="switch" :displayable="item.c_work_status != '5'" />

                <eui-card-input v-if="item.c_work_status >= '5' && item.c_is_abnormal == '1'"
                    v-model="item.c_recheck_memo" title="复检说明" type="textarea" placeholder="请录入复检说明" maxlength="200"
                    :displayable="item.c_work_status != '5'" />
            </eui-base-card>
            <eui-base-card title='图片上传'>
                <UploadFile :autoUpload="false" :disabled="disabled" :readonly="disabled" fileMediatype="image"
                            :fileId="fileId" fileClass="巡检任务单" return-type="array" mode="grid">
                </UploadFile>
				<!-- <UploadFile :autoUpload="false" fileMediatype="image" :fileId="fileId" fileClass="巡检任务单"
					return-type="array" mode="grid" ></UploadFile> -->
			</eui-base-card>
        </eui-page-content>
        <template #bottom>
            <view class="bt-button" v-if="bottomHeight > 0">
                <eui-button v-if="workStatus == '1'" type="primary" title="保存" @click="submit" />
                <eui-button v-else-if="workStatus == '5'" type="primary" title="复检完成" @click="recheck" />
            </view>
        </template>
    </eui-page-frame>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { computed, ref } from 'vue';
import UploadFile from '@/components/UploadFile.vue';
import useService from '@/hooks/useService';
import type { patrolTaskg } from '../types/propertyTypes/property.d.ts';

const service = useService({ prefix: "apiApi" });
const uniqueKey = '管易通物业模块服务';

const title = ref('巡场详情');
const theme = ref('');
const id = ref('');
const pointId = ref('');
const maxSort = ref(1);

const showdata = ref([{
    // c_edit_style: '0',
    // c_selected: '',
    // c_title: '',
    // c_option_list: [],
    // c_displayable: false,
    // c_value: '',
    // c_input_type: '',
    // c_is_abnormal: '0', //是否异常
    // c_rectify_memo: '',
    // c_is_rectify:'0',  //是否整改
    // c_is_recheck:'0', //是否复检
    // c_recheck_memo: '',
    // c_work_status:'0'
}])

const bottomHeight = computed(() => {
    const abnormalItem = showdata.value.filter(x => x.c_is_abnormal == '1');
    if (workStatus.value == '1' || (workStatus.value == '5' && abnormalItem.length > 0)) {
        return 124
    }
    else {
        return 0;
    }
})

const disabled = computed(() => {
    const [first] = showdata.value;
    return first?.c_displayable;
})

const workStatus = computed(() => {
    const [first] = showdata.value;
    return first?.c_work_status;// c_work_status: 0：待确认 1：待处理 2：已作废 3：待评价 4：已完成 5：待整改 6：已整改 7：复检完成 9：已驳回
})

onLoad((req) => {
    id.value = req?.id;
    pointId.value = req?.pointId;
    maxSort.value = req?.maxSort || 1;
    title.value = '' + pointId.value + '巡场详情';
    theme.value = decodeURIComponent(req?.theme);
    showdata.value = JSON.parse(decodeURIComponent(req?.data));
})

const fileId = computed(() => {
    return id.value + '-' + pointId.value;
});

/**
 * 提交
 */
const submit = async () => {
    if (showdata.value.find(x => !x.c_value && x.c_required == '1')) {
        var empty = showdata.value.find(x => !x.c_value && x.c_required == '1');
        uni.showToast({
            duration: 2000,
            icon: 'none',
            title: '请录入' + empty?.c_title,
            mask: true,
        });
        return;
    }
    console.log('showdata', showdata.value);
    const dataToSave: patrolTaskg[] = showdata.value.map(x => {
        return {
            c_tenant_id: x.tenant_id,
            c_id: id.value,
            c_sort: ++maxSort.value,
            c_point_id: pointId.value,
            c_sub_id: x.c_sub_id,
            c_item_id: x.c_item_id,
            c_value: x.c_value,
            c_is_abnormal: x.c_is_abnormal ? '1' : '0',
            c_penalty: x.c_penalty,
            c_rectify_memo: x.c_rectify_memo || '',
        }
    })

    const toSaveData = {
        id: id.value,
        pointId: pointId.value,
        patrolPointList: dataToSave
    }

    uni.showLoading({ title: "处理中...", mask: true });
    const result = await service.request(uniqueKey, 'SavePatrolPointData', toSaveData);
    uni.hideLoading();
    if (result) {
        uni.showToast({
            duration: 3500,
            icon: 'none',
            title: '保存成功!',
        }).then(() => {
            uni.navigateBack();
        });
    }
}
/**
 * 复检完成
 */
const recheck = async () => {
    if (showdata.value.find(x => x.c_is_abnormal && x.c_is_recheck != '1')) {
        uni.showToast({
            duration: 2000,
            icon: 'none',
            title: '存在异常项目没有复检完成，请确认',
            mask: true,
        });
        return;
    }

    const dataToSave: patrolTaskg[] = showdata.value.map(x => {
        return {
            c_tenant_id: x.tenant_id,
            c_id: id.value,
            c_sort: x.c_sort,
            c_point_id: pointId.value,
            c_sub_id: x.c_sub_id,
            c_item_id: x.c_item_id,
            c_value: x.c_value,
            c_is_abnormal: x.c_is_abnormal ? '1' : '0',
            c_penalty: x.c_penalty,
            c_rectify_memo: x.c_rectify_memo || '',
            c_is_rectify: x.c_is_rectify ? '1' : '0',
            c_deadline: x.c_deadline,
            c_rectify_provider: x.c_rectify_provider,
            c_rectify_userno: x.c_rectify_userno,
            c_is_recheck: x.c_is_recheck ? '1' : '0',
            c_recheck_memo: x.c_recheck_memo || '',
        }
    })

    console.log('dataToSave', dataToSave);
    const toSaveData = {
        id: id.value,
        pointId: pointId.value,
        patrolPointList: dataToSave
    }

    uni.showLoading({ title: "处理中...", mask: true });
    const result = await service.request(uniqueKey, 'SavePatrolPointData', toSaveData);
    uni.hideLoading();
    if (result) {
        uni.showToast({
            duration: 3500,
            icon: 'none',
            title: '处理成功!',
        }).then(() => {
            uni.navigateBack();
        });
    }
}

</script>

<style lang="scss" scoped>
.navigation {
    background: #FFFFFF;
    flex: 0 0 auto;
    margin-bottom: 0rpx;
}

.bt-button {
    background-color: white;
    padding: 20rpx;
    height: 80rpx;
    width: 96%;
    bottom: 20rpx;
    right: 0rpx;
    position: fixed;
    margin-bottom: -20rpx;
    display: flex;
    justify-content: space-between;
}

::v-deep .e-card view:first-of-type {
    border-radius: 20rpx;
}

::v-deep .m-card {
    &-radio-view {
        padding: 0 !important;
    }
    &-input-view {
        padding: 28rpx 0 !important;
    }
}

::v-deep .e-card__content {
    margin-left: 10rpx ;
}

</style>